package SpringCloudBase.controller;

import SpringCloudBase.vo.UserVO;
import com.netflix.discovery.EurekaClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

/**
 * @ClassName ConsumerOrderController
 * @Description : 功能说明
 * @Author : LiangT
 * @Date : 2020/3/31 19:12
 */
@RestController
public class ConsumerOrderController {
    @Autowired
    private EurekaClient eurekaClient;

    @Autowired
    private RestTemplate restTemplate;
    @GetMapping("/order/{id}")
    @HystrixCommand(fallbackMethod = "getUserError")
    public UserVO getUser(@PathVariable Long id){
//        验证EurekaClient访问其他服务
       String url = eurekaClient.getNextServerFromEureka("PROVIDER-USER",true).getHomePageUrl()+"/user/"+id;
        System.out.println("请求访问地址="+url);
//        验证使用Ribbon访问其他服务
        return restTemplate.getForObject(url,UserVO.class);
    }

    public UserVO getUserError(Long id){
        UserVO userVO = new UserVO();
        userVO.setDate(new Date());
        userVO.setId(10001L);
        return userVO;
    }
}
